﻿using System;
using System.IO;
using System.Text;
using System.Web.UI;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Xpedite.Resources;
using Xpedite.Resources.Builders;
using Xpedite.Resources.Combiners;
using Xpedite.Resources.Definitions;
using IResourceUrlGenerator = Xpedite.Resources.IResourceUrlGenerator;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Test.Resources.Definitions
{
  [TestClass]
  public class CssDefinitionTest
  {
    private readonly Mock<IResourceBuilderFactory> _builderFactory = new Mock<IResourceBuilderFactory>(MockBehavior.Strict);
    private readonly Mock<IResourceUrlGenerator> _urlGenerator = new Mock<IResourceUrlGenerator>(MockBehavior.Strict);
    private readonly Mock<ICombinerFactory> _combinerFactory = new Mock<ICombinerFactory>(MockBehavior.Strict);
    private readonly Mock<IResourceContainer> _resource = new Mock<IResourceContainer>(MockBehavior.Strict);
    private readonly Mock<IResourceUrl> _virtualUrl = new Mock<IResourceUrl>(MockBehavior.Strict);
    private readonly Mock<IResourceCache> _cache = new Mock<IResourceCache>(MockBehavior.Strict);
    private readonly Mock<ICombiner> _combiner = new Mock<ICombiner>(MockBehavior.Strict);
    private readonly DefinitionBase _definition;

    public CssDefinitionTest()
    {
      _definition = new CssDefinition(new DefinitionArgs(_cache.Object, _urlGenerator.Object, _builderFactory.Object, _combinerFactory.Object), "ReferenceName");
    }

    [TestMethod]
    public void TestResourceTypeCss()
    {
      Assert.AreEqual(ResourceType.Css, _definition.ResourceType);
    }

    [TestMethod]
    public void TestEmitResource()
    {
      var capturedFactory = new CapturedValue<Func<IResourceContainer>>();
      var html = new StringBuilder();

      _virtualUrl.SetupGet(mock => mock.VirtualPath).Returns("/VirtualDirectory/SomeFile.ext");
      _urlGenerator.Setup(mock => mock.GenerateUrlFor(_definition)).Returns(_virtualUrl.Object);
      _cache.Setup(mock => mock.GetOrCreateWithResourceDependencies(_virtualUrl.Object, It.Is(capturedFactory.Match), _definition.Resources)).Returns(_resource.Object);
      
      using(var stringWriter = new StringWriter(html))
      using(var htmlWriter = new HtmlTextWriter(stringWriter))
        _definition.EmitResource(htmlWriter);

      //Verify Emit
      Assert.AreEqual("<link type=\"text/css\" rel=\"stylesheet\" href=\"/VirtualDirectory/SomeFile.ext\" />", html.ToString());
      Assert.IsNotNull(capturedFactory.Value);

      //Verify Factory
      _combinerFactory.Setup(mock => mock.CreateCssCombiner()).Returns(_combiner.Object);
      _combiner.Setup(mock => mock.Combine(_virtualUrl.Object, _definition)).Returns(_resource.Object);

      capturedFactory.Value.Invoke();
    }
  }
}
